<template>
	<div class="app-container" v-loading="data.mtloading">
		<el-form :model="data.info" :rules="data.rules" class="w-full" label-width="120" ref="ruleFormRef">

			<el-form-item :label="$t('account.role.name') + ':'" prop="name" class="w-1/2">
				<el-input v-model="data.info.name" :placeholder="$t('message.pleaseEnter')" />
			</el-form-item>

			<el-form-item :label="$t('account.role.roleSort') + ':'" prop="sort" class="w-1/2">
				<el-input v-model="data.info.sort" :placeholder="$t('message.pleaseEnter')" />
			</el-form-item>

			<el-form-item :label="$t('account.role.roleDesc') + ':'" prop="desc" class="w-1/2">
				<el-input v-model="data.info.desc" :placeholder="$t('message.pleaseEnter')" />
			</el-form-item>

			<el-form-item :label="$t('account.role.roleStatus') + ':'" prop="status" class="w-1/2">
				<el-select v-model="data.info.status" :placeholder="$t('message.pleaseSelect')">
					<el-option :label="$t('public.enable')" :value="1" />
					<el-option :label="$t('public.prohibit')" :value="2" />
				</el-select>
			</el-form-item>

			<div class="w-2/4 text-center">
				<div class="inline-block">
					<el-button type="primary" @click="submit(ruleFormRef)">{{ $t('public.save') }}</el-button>
					<el-button @click="cancell">{{ $t('public.cancel') }}</el-button>
				</div>
			</div>

		</el-form>
	</div>
</template>

<script lang="ts" setup>
import { useI18n } from 'vue-i18n'
const { t: $t } = useI18n()
import { updateauthrole } from '@/api/account';
import { useRouter, useRoute } from "vue-router";
let ruleFormRef = ref<any>();
const router = useRouter()
const route = useRoute()
let data = reactive({
	mtloading: false, // 加载
	info: { // 主体信息
		role_id: '',
		name: '',
		desc: '',
		sort: 1,
		status: 1,
	},
	rules: { // 表单验证
		name: { required: true, message: $t('message.pleaseEnter'), trigger: 'blur' },
	}
})
// 取消
let cancell = () => {
	getinfo()
	router.back()
}
// 获取主体信息
let getinfo = () => {
	let info = JSON.parse(decodeURI(route.query.l)).info
	for (let key in data.info) {
		data.info[key] = info[key]
	}
}

// 保存
let submit = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid, fields) => {
		if (valid) {
			data.mtloading = true
			updateauthrole(data.info).then(res => {
				data.mtloading = false
				if (res.state == 200) {
					ElMessage({
						message: $t('message.modifySuccess'),
						type: 'success'
					})
					router.back()
					return
				}
				ElMessage({
					message: $t('message.networkFailed'),
					type: 'error'
				})
			})
		}
	})
}

getinfo()

</script>